<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" />
<title>The decorator module</title>
<meta name="author" content="Michele Simionato" />
</head>
<body>
<div class="document" id="the-decorator-module">
<h1 class="title">The <tt class="docutils literal"><span class="pre">decorator</span></tt> module</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>Michele Simionato</td></tr>
<tr class="field"><th class="docinfo-name">E-mail:</th><td class="field-body"><a class="reference" href="mailto:michele.simionato&#64;gmail.com">michele.simionato&#64;gmail.com</a></td>
</tr>
<tr><th class="docinfo-name">Version:</th>
<td>2.0</td></tr>
<tr class="field"><th class="docinfo-name">Download page:</th><td class="field-body"><a class="reference" href="http://www.phyast.pitt.edu/~micheles/python/decorator.zip">http://www.phyast.pitt.edu/~micheles/python/decorator.zip</a></td>
</tr>
<tr class="field"><th class="docinfo-name">Installation:</th><td class="field-body"><tt class="docutils literal"><span class="pre">easy_install</span> <span class="pre">decorator</span></tt></td>
</tr>
<tr class="field"><th class="docinfo-name">License:</th><td class="field-body">Python license</td>
</tr>
</tbody>
</table>
<div class="contents topic">
<p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
<ul class="simple">
<li><a class="reference" href="#introduction" id="id3" name="id3">Introduction</a></li>
<li><a class="reference" href="#definitions" id="id4" name="id4">Definitions</a></li>
<li><a class="reference" href="#statement-of-the-problem" id="id5" name="id5">Statement of the problem</a></li>
<li><a class="reference" href="#the-solution" id="id6" name="id6">The solution</a></li>
<li><a class="reference" href="#decorator-is-a-decorator" id="id7" name="id7"><tt class="docutils literal"><span class="pre">decorator</span></tt> is a decorator</a></li>
<li><a class="reference" href="#memoize" id="id8" name="id8"><tt class="docutils literal"><span class="pre">memoize</span></tt></a></li>
<li><a class="reference" href="#locked" id="id9" name="id9"><tt class="docutils literal"><span class="pre">locked</span></tt></a></li>
<li><a class="reference" href="#delayed-and-threaded" id="id10" name="id10"><tt class="docutils literal"><span class="pre">delayed</span></tt> and <tt class="docutils literal"><span class="pre">threaded</span></tt></a></li>
<li><a class="reference" href="#blocking" id="id11" name="id11"><tt class="docutils literal"><span class="pre">blocking</span></tt></a></li>
<li><a class="reference" href="#redirecting-stdout" id="id12" name="id12"><tt class="docutils literal"><span class="pre">redirecting_stdout</span></tt></a></li>
<li><a class="reference" href="#tail-recursive" id="id13" name="id13"><tt class="docutils literal"><span class="pre">tail_recursive</span></tt></a></li>
<li><a class="reference" href="#caveats-and-limitations" id="id14" name="id14">Caveats and limitations</a></li>
<li><a class="reference" href="#licence" id="id15" name="id15">LICENCE</a></li>
</ul>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id3" id="introduction" name="introduction">Introduction</a></h1>
<p>Python 2.4 decorators are an interesting example of why syntactic sugar
matters: in principle, their introduction changed nothing, since they do
not provide any new functionality which was not already present in the
language; in practice, their introduction has significantly changed the way
we structure our programs in Python. I believe the change is for the best,
and that decorators are a great idea since:</p>
<ul class="simple">
<li>decorators help reducing boilerplate code;</li>
<li>decorators help separation of concerns;</li>
<li>decorators enhance readability and maintenability;</li>
<li>decorators are very explicit.</li>
</ul>
<p>Still, as of now, writing custom decorators correctly requires
some experience and it is not as easy as it could be. For instance,
typical implementations of decorators involve nested functions, and
we all know that flat is better than nested.</p>
<p>The aim of the <tt class="docutils literal"><span class="pre">decorator</span></tt> module it to simplify the usage of
decorators for the average programmer, and to popularize decorators
usage giving examples of useful decorators, such as <tt class="docutils literal"><span class="pre">memoize</span></tt>,
<tt class="docutils literal"><span class="pre">tracing</span></tt>, <tt class="docutils literal"><span class="pre">redirecting_stdout</span></tt>, <tt class="docutils literal"><span class="pre">locked</span></tt>, etc.</p>
<p>The core of this module is a decorator factory called <tt class="docutils literal"><span class="pre">decorator</span></tt>.
All decorators discussed here are built as simple recipes on top
of  <tt class="docutils literal"><span class="pre">decorator</span></tt>. You may find their source code in the <tt class="docutils literal"><span class="pre">_main.py</span></tt>
file, which is generated automatically when you run the doctester
(included into the decorator package) on this documentation:</p>
<pre class="literal-block">
$ python doctester.py documentation.txt
</pre>
<p>At the same time the doctester runs all the examples contained here as
test cases.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id4" id="definitions" name="definitions">Definitions</a></h1>
<p>Technically speaking, any Python object which can be called with one argument
can be used as  a decorator. However, this definition is somewhat too large
to be really useful. It is more convenient to split the generic class of
decorators in two groups:</p>
<ul class="simple">
<li><em>signature-preserving</em> decorators, i.e. callable objects taking a
function as input and returning a function <em>with the same
signature</em> as output;</li>
<li><em>signature-changing</em> decorators, i.e. decorators that change
the signature of their input function, or decorators returning
non-callable objects.</li>
</ul>
<p>Signature-changing decorators have their use: for instance the
builtin classes <tt class="docutils literal"><span class="pre">staticmethod</span></tt> and <tt class="docutils literal"><span class="pre">classmethod</span></tt> are in this
group, since they take functions and return descriptor objects which
are not functions, nor callables.</p>
<p>However, signature-preserving decorators are more common and easier to
reason about; in particular signature-preserving decorators can be
composed together whereas other
decorators in general cannot (for instance you cannot
meaningfully compose a staticmethod with a classmethod or viceversa).</p>
<p>Writing signature-preserving decorators from scratch is not that
obvious, especially if one wants to define proper decorators that
can accept functions with any signature. A simple example will clarify
the issue.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id5" id="statement-of-the-problem" name="statement-of-the-problem">Statement of the problem</a></h1>
<p>Suppose you want to trace a function: this is a typical use case
for a decorator and you can find in many places code like this:</p>
<pre class="literal-block">
#&lt;_main.py&gt;

try:
    from functools import update_wrapper
except ImportError: # using Python version &lt; 2.5
    def decorator_trace(f):
        def newf(*args, **kw):
           print &quot;calling %s with args %s, %s&quot; % (f.__name__, args, kw)
           return f(*args, **kw)
        newf.__name__ = f.__name__
        newf.__dict__.update(f.__dict__)
        newf.__doc__ = f.__doc__
        newf.__module__ = f.__module__
        return newf
else: # using Python 2.5+
    def decorator_trace(f):
        def newf(*args, **kw):
            print &quot;calling %s with args %s, %s&quot; % (f.__name__, args, kw)
            return f(*args, **kw)
        return update_wrapper(newf, f)

#&lt;/_main.py&gt;
</pre>
<p>The implementation above works in the sense that the decorator
can accept functions with generic signatures; unfortunately this
implementation does <em>not</em> define a signature-preserving decorator, since in
general <tt class="docutils literal"><span class="pre">decorator_trace</span></tt> returns a function with a
<em>different signature</em> from the original function.</p>
<p>Consider for instance the following case:</p>
<pre class="doctest-block">
&gt;&gt;&gt; &#64;decorator_trace
... def f1(x):
...     pass
</pre>
<p>Here the original function takes a single argument named <tt class="docutils literal"><span class="pre">x</span></tt>,
but the decorated function takes any number of arguments and
keyword arguments:</p>
<pre class="doctest-block">
&gt;&gt;&gt; from inspect import getargspec
&gt;&gt;&gt; print getargspec(f1)
([], 'args', 'kw', None)
</pre>
<p>This means that introspection tools such as pydoc will give
wrong informations about the signature of <tt class="docutils literal"><span class="pre">f1</span></tt>. This is pretty bad:
pydoc will tell you that the function accepts a generic signature
<tt class="docutils literal"><span class="pre">*args</span></tt>, <tt class="docutils literal"><span class="pre">**kw</span></tt>, but when you try to call the function with more than an
argument, you will get an error:</p>
<pre class="doctest-block">
&gt;&gt;&gt; f1(0, 1)
Traceback (most recent call last):
   ...
TypeError: f1() takes exactly 1 argument (2 given)
</pre>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id6" id="the-solution" name="the-solution">The solution</a></h1>
<p>The solution is to provide a generic factory of generators, which
hides the complexity of making signature-preserving decorators
from the application programmer. The <tt class="docutils literal"><span class="pre">decorator</span></tt> factory
allows to define decorators without the need to use nested functions
or classes. As an example, here is how you can define
<tt class="docutils literal"><span class="pre">decorator_trace</span></tt>.</p>
<p>First of all, you must import <tt class="docutils literal"><span class="pre">decorator</span></tt>:</p>
<pre class="doctest-block">
&gt;&gt;&gt; from decorator import decorator
</pre>
<p>Then you must define an helper function with signature <tt class="docutils literal"><span class="pre">(f,</span> <span class="pre">*args,</span> <span class="pre">**kw)</span></tt>
which calls the original function <tt class="docutils literal"><span class="pre">f</span></tt> with arguments <tt class="docutils literal"><span class="pre">args</span></tt> and <tt class="docutils literal"><span class="pre">kw</span></tt>
and implements the tracing capability:</p>
<pre class="literal-block">
#&lt;_main.py&gt;

def trace(f, *args, **kw):
    print &quot;calling %s with args %s, %s&quot; % (f.func_name, args, kw)
    return f(*args, **kw)

#&lt;/_main.py&gt;
</pre>
<p><tt class="docutils literal"><span class="pre">decorator</span></tt> is able to convert the helper function into a
signature-preserving decorator
object, i.e  is a callable object that takes a function and returns a
decorated function with the same signature of the original function.
Therefore, you can write the following:</p>
<pre class="doctest-block">
&gt;&gt;&gt; &#64;decorator(trace)
... def f1(x):
...     pass
</pre>
<p>It is immediate to verify that <tt class="docutils literal"><span class="pre">f1</span></tt> works</p>
<pre class="doctest-block">
&gt;&gt;&gt; f1(0)
calling f1 with args (0,), {}
</pre>
<p>and it that it has the correct signature:</p>
<pre class="doctest-block">
&gt;&gt;&gt; print getargspec(f1)
(['x'], None, None, None)
</pre>
<p>The same decorator works with functions of any signature:</p>
<pre class="doctest-block">
&gt;&gt;&gt; &#64;decorator(trace)
... def f(x, y=1, z=2, *args, **kw):
...     pass
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; f(0, 3)
calling f with args (0, 3, 2), {}
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; print getargspec(f)
(['x', 'y', 'z'], 'args', 'kw', (1, 2))
</pre>
<p>That includes even functions with exotic signatures like the following:</p>
<pre class="doctest-block">
&gt;&gt;&gt; &#64;decorator(trace)
... def exotic_signature((x, y)=(1,2)): return x+y
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; print getargspec(exotic_signature)
([['x', 'y']], None, None, ((1, 2),))
&gt;&gt;&gt; exotic_signature()
calling exotic_signature with args ((1, 2),), {}
3
</pre>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id7" id="decorator-is-a-decorator" name="decorator-is-a-decorator"><tt class="docutils literal"><span class="pre">decorator</span></tt> is a decorator</a></h1>
<p>The <tt class="docutils literal"><span class="pre">decorator</span></tt> factory itself can be considered as a signature-changing
decorator, just as <tt class="docutils literal"><span class="pre">classmethod</span></tt> and <tt class="docutils literal"><span class="pre">staticmethod</span></tt>.
However, <tt class="docutils literal"><span class="pre">classmethod</span></tt> and <tt class="docutils literal"><span class="pre">staticmethod</span></tt> return generic
objects which are not callable, while <tt class="docutils literal"><span class="pre">decorator</span></tt> returns
signature-preserving decorators, i.e. functions of a single argument.
Therefore, you can write</p>
<pre class="doctest-block">
&gt;&gt;&gt; &#64;decorator
... def tracing(f, *args, **kw):
...     print &quot;calling %s with args %s, %s&quot; % (f.func_name, args, kw)
...     return f(*args, **kw)
</pre>
<p>and this idiom is actually redefining <tt class="docutils literal"><span class="pre">tracing</span></tt> to be a decorator.
We can easily check that the signature has changed:</p>
<pre class="doctest-block">
&gt;&gt;&gt; print getargspec(tracing)
(['f'], None, None, None)
</pre>
<p>Therefore now <tt class="docutils literal"><span class="pre">tracing</span></tt> can be used as a decorator and
the following will work:</p>
<pre class="doctest-block">
&gt;&gt;&gt; &#64;tracing
... def func(): pass
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; func()
calling func with args (), {}
</pre>
<p>BTW, you may use the decorator on lambda functions too:</p>
<pre class="doctest-block">
&gt;&gt;&gt; tracing(lambda : None)()
calling &lt;lambda&gt; with args (), {}
</pre>
<p>For the rest of this document, I will discuss examples of useful
decorators built on top of <tt class="docutils literal"><span class="pre">decorator</span></tt>.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id8" id="memoize" name="memoize"><tt class="docutils literal"><span class="pre">memoize</span></tt></a></h1>
<p>This decorator implements the <tt class="docutils literal"><span class="pre">memoize</span></tt> pattern, i.e. it caches
the result of a function in a dictionary, so that the next time
the function is called with the same input parameters the result is retrieved
from the cache and not recomputed.</p>
<pre class="literal-block">
#&lt;_main.py&gt;

from decorator import *

def getattr_(obj, name, default_thunk):
    &quot;Similar to .setdefault in dictionaries.&quot;
    try:
        return getattr(obj, name)
    except AttributeError:
        default = default_thunk()
        setattr(obj, name, default)
        return default

&#64;decorator
def memoize(func, *args):
    dic = getattr_(func, &quot;memoize_dic&quot;, dict)
    # memoize_dic is created at the first call
    if args in dic:
        return dic[args]
    else:
        result = func(*args)
        dic[args] = result
        return result

#&lt;/_main.py&gt;
</pre>
<p>Here is a test of usage:</p>
<pre class="doctest-block">
&gt;&gt;&gt; &#64;memoize
... def heavy_computation():
...     time.sleep(2)
...     return &quot;done&quot;
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; print heavy_computation() # the first time it will take 2 seconds
done
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; print heavy_computation() # the second time it will be instantaneous
done
</pre>
<p>As an exercise, try to implement <tt class="docutils literal"><span class="pre">memoize</span></tt> <em>properly</em> without the
<tt class="docutils literal"><span class="pre">decorator</span></tt> factory.</p>
<p>Notice that this <tt class="docutils literal"><span class="pre">memoize</span></tt> only works for functions with no keyword
arguments, since it is impossible to memoize correctly something that
depends on mutable arguments. One can relax this requirement, and
allow keyword arguments in the signature: however, if keyword
arguments are passed, the result should not be cached. For an
example see <a class="reference" href="http://www.python.org/moin/PythonDecoratorLibrary">http://www.python.org/moin/PythonDecoratorLibrary</a></p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id9" id="locked" name="locked"><tt class="docutils literal"><span class="pre">locked</span></tt></a></h1>
<p>There are good use cases for decorators is in multithreaded programming.
For instance, a <tt class="docutils literal"><span class="pre">locked</span></tt> decorator can remove the boilerplate
for acquiring/releasing locks <a class="footnote-reference" href="#id2" id="id1" name="id1">[1]</a>.</p>
<pre class="literal-block">
#&lt;_main.py&gt;

import threading

&#64;decorator
def locked(func, *args, **kw):
    lock = getattr_(func, &quot;lock&quot;, threading.Lock)
    lock.acquire()
    try:
        result = func(*args, **kw)
    finally:
        lock.release()
    return result

#&lt;/_main.py&gt;
</pre>
<table class="docutils footnote" frame="void" id="id2" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1" name="id2">[1]</a></td><td>In Python 2.5, the preferred way to manage locking is via
the <tt class="docutils literal"><span class="pre">with</span></tt> statement: <a class="reference" href="http://docs.python.org/dev/lib/with-locks.html">http://docs.python.org/dev/lib/with-locks.html</a></td></tr>
</tbody>
</table>
<p>To show an example of usage, suppose one wants to write some data to
an external resource which can be accessed by a single user at once
(for instance a printer). Then the access to the writing function must
be locked:</p>
<pre class="literal-block">
#&lt;_main.py&gt;

import time

datalist = [] # for simplicity the written data are stored into a list.

&#64;locked
def write(data):
    &quot;Writing to a sigle-access resource&quot;
    time.sleep(1)
    datalist.append(data)

#&lt;/_main.py&gt;
</pre>
<p>Since the writing function is locked, we are guaranteed that at any given time
there is at most one writer. An example multithreaded program that invokes
<tt class="docutils literal"><span class="pre">write</span></tt> and prints the datalist is shown in the next section.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id10" id="delayed-and-threaded" name="delayed-and-threaded"><tt class="docutils literal"><span class="pre">delayed</span></tt> and <tt class="docutils literal"><span class="pre">threaded</span></tt></a></h1>
<p>Often, one wants to define families of decorators, i.e. decorators depending
on one or more parameters.</p>
<p>Here I will consider the example of a one-parameter family of <tt class="docutils literal"><span class="pre">delayed</span></tt>
decorators taking a procedure and converting it into a delayed procedure.
In this case the time delay is the parameter.</p>
<p>A delayed procedure is a procedure that, when called,
is executed in a separate thread after a certain time
delay. The implementation is not difficult:</p>
<pre class="literal-block">
#&lt;_main.py&gt;

def delayed(nsec):
    def call(proc, *args, **kw):
        thread = threading.Timer(nsec, proc, args, kw)
        thread.start()
        return thread
    return decorator(call)

#&lt;/_main.py&gt;
</pre>
<p>Notice that without the help of <tt class="docutils literal"><span class="pre">decorator</span></tt>, an additional level of
nesting would have been needed.</p>
<p>Delayed decorators as intended to be used on procedures, i.e.
on functions returning <tt class="docutils literal"><span class="pre">None</span></tt>, since the return value of the original
function is discarded by this implementation. The decorated function returns
the current execution thread, which can be stored and checked later, for
instance to verify that the thread <tt class="docutils literal"><span class="pre">.isAlive()</span></tt>.</p>
<p>Delayed procedures can be useful in many situations. For instance, I have used
this pattern to start a web browser <em>after</em> the web server started,
in code such as</p>
<pre class="doctest-block">
&gt;&gt;&gt; &#64;delayed(2)
... def start_browser():
...     &quot;code to open an external browser window here&quot;
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; #start_browser() # will open the browser in 2 seconds
&gt;&gt;&gt; #server.serve_forever() # enter the server mainloop
</pre>
<p>The particular case in which there is no delay is important enough
to deserve a name:</p>
<pre class="literal-block">
#&lt;_main.py&gt;

threaded = delayed(0) # no-delay decorator

#&lt;/_main.py&gt;
</pre>
<p>Threaded procedures will be executed in a separated thread as soon
as they are called. Here is an example using the <tt class="docutils literal"><span class="pre">write</span></tt>
routine defined before:</p>
<pre class="doctest-block">
&gt;&gt;&gt; &#64;threaded
... def writedata(data):
...     write(data)
</pre>
<p>Each call to <tt class="docutils literal"><span class="pre">writedata</span></tt> will create a new writer thread, but there will
be no synchronization problems since <tt class="docutils literal"><span class="pre">write</span></tt> is locked.</p>
<pre class="doctest-block">
&gt;&gt;&gt; writedata(&quot;data1&quot;)
&lt;_Timer(Thread-1, started)&gt;
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; time.sleep(.1) # wait a bit, so we are sure data2 is written after data1
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; writedata(&quot;data2&quot;)
&lt;_Timer(Thread-2, started)&gt;
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; time.sleep(2) # wait for the writers to complete
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; print datalist
['data1', 'data2']
</pre>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id11" id="blocking" name="blocking"><tt class="docutils literal"><span class="pre">blocking</span></tt></a></h1>
<p>Sometimes one has to deal with blocking resources, such as <tt class="docutils literal"><span class="pre">stdin</span></tt>, and
sometimes it is best to have back a &quot;busy&quot; message than to block everything.
This behavior can be implemented with a suitable decorator:</p>
<pre class="literal-block">
#&lt;_main.py&gt;

def blocking(not_avail=&quot;Not Available&quot;):
    def call(f, *args, **kw):
        if not hasattr(f, &quot;thread&quot;): # no thread running
            def set_result(): f.result = f(*args, **kw)
            f.thread = threading.Thread(None, set_result)
            f.thread.start()
            return not_avail
        elif f.thread.isAlive():
            return not_avail
        else: # the thread is ended, return the stored result
            del f.thread
            return f.result
    return decorator(call)

#&lt;/_main.py&gt;
</pre>
<p>Functions decorated with <tt class="docutils literal"><span class="pre">blocking</span></tt> will return a busy message if
the resource is unavailable, and the intended result if the resource is
available. For instance:</p>
<pre class="doctest-block">
&gt;&gt;&gt; &#64;blocking(&quot;Please wait ...&quot;)
... def read_data():
...     time.sleep(3) # simulate a blocking resource
...     return &quot;some data&quot;
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; print read_data() # data is not available yet
Please wait ...
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; time.sleep(1)
&gt;&gt;&gt; print read_data() # data is not available yet
Please wait ...
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; time.sleep(1)
&gt;&gt;&gt; print read_data() # data is not available yet
Please wait ...
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; time.sleep(1.1) # after 3.1 seconds, data is available
&gt;&gt;&gt; print read_data()
some data
</pre>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id12" id="redirecting-stdout" name="redirecting-stdout"><tt class="docutils literal"><span class="pre">redirecting_stdout</span></tt></a></h1>
<p>Decorators help in removing the boilerplate associated to <tt class="docutils literal"><span class="pre">try</span> <span class="pre">..</span> <span class="pre">finally</span></tt>
blocks. We saw the case of <tt class="docutils literal"><span class="pre">locked</span></tt>; here is another example:</p>
<pre class="literal-block">
#&lt;_main.py&gt;

import sys

def redirecting_stdout(new_stdout):
    def call(func, *args, **kw):
        save_stdout = sys.stdout
        sys.stdout = new_stdout
        try:
            result = func(*args, **kw)
        finally:
            sys.stdout = save_stdout
        return result
    return decorator(call)

#&lt;/_main.py&gt;
</pre>
<p>Here is an example of usage:</p>
<pre class="doctest-block">
&gt;&gt;&gt; from StringIO import StringIO
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; out = StringIO()
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; &#64;redirecting_stdout(out)
... def helloworld():
...     print &quot;hello, world!&quot;
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; helloworld()
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; out.getvalue()
'hello, world!\n'
</pre>
<p>Similar tricks can be used to remove the boilerplate associate with
transactional databases. I think you got the idea, so I will leave
the transactional example as an exercise for the reader. Of course
in Python 2.5 these use cases can also be addressed with the <tt class="docutils literal"><span class="pre">with</span></tt>
statement.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id13" id="tail-recursive" name="tail-recursive"><tt class="docutils literal"><span class="pre">tail_recursive</span></tt></a></h1>
<p>Sometimes, you may find on the net cool decorators that you would
like to include in your code. However, it is likely these decorators
have not been written using <tt class="docutils literal"><span class="pre">decorator</span></tt> and that they are not
signature-preserving. So, you need an easy way to convert
third party decorators in signature-preserving decorators without
having to rewrite them in terms of <tt class="docutils literal"><span class="pre">decorator</span></tt>. Luckily, the
<tt class="docutils literal"><span class="pre">decorator</span></tt> module provides a convenience function <tt class="docutils literal"><span class="pre">update_wrapper</span></tt>
which can be used for this purpose. By default, <tt class="docutils literal"><span class="pre">update_wrapper</span></tt>
mimic the behavior of <tt class="docutils literal"><span class="pre">functools.update_wrapper</span></tt> which has been
added to the standard library in Python 2.5 (i.e. it just copies the
function attributes <tt class="docutils literal"><span class="pre">__name__</span></tt>, <tt class="docutils literal"><span class="pre">__doc__</span></tt>, <tt class="docutils literal"><span class="pre">__dict__</span></tt> and
<tt class="docutils literal"><span class="pre">__module__</span></tt>), but is has the additional power to copy the signature,
simply by setting the flag <tt class="docutils literal"><span class="pre">create=True</span></tt>.</p>
<p>For instance, suppose you have a function <tt class="docutils literal"><span class="pre">func</span></tt>
with a &quot;permissive&quot; signature (say <tt class="docutils literal"><span class="pre">func(*args,</span> <span class="pre">**kw)</span></tt>, returned by the
third party non signature-preserving decorator) and another
function <tt class="docutils literal"><span class="pre">model</span></tt> which has a stricter signature; then
<tt class="docutils literal"><span class="pre">update_wrapper(func,</span> <span class="pre">model,</span> <span class="pre">create=True)</span></tt>
returns a copy of <tt class="docutils literal"><span class="pre">func</span></tt> with signature copied from <tt class="docutils literal"><span class="pre">model</span></tt>. Notice that
it is your responsability to make sure that the original function and the model
function have compatibile signature, i.e. that the signature of
the model is stricter (or equivalent) than the signature of the
original function. If not, you will get an error at function calling
time.</p>
<p>In order to give an example of usage for <tt class="docutils literal"><span class="pre">update_wrapper</span></tt>, I will show a
pretty slick decorator that converts a tail-recursive function in an iterative
function. I have shamelessly stolen the basic idea from Kay Schluehr's recipe
in the Python Cookbook,
<a class="reference" href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691</a>.</p>
<pre class="literal-block">
#&lt;_main.py&gt;

from decorator import update_wrapper

class TailRecursive(object):
    &quot;&quot;&quot;
    tail_recursive decorator based on Kay Schluehr's recipe
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
    &quot;&quot;&quot;
    CONTINUE = object() # sentinel

    def __init__(self, func):
        self.func = func
        self.firstcall = True

    def __call__(self, *args, **kwd):
        try:
            if self.firstcall: # start looping
                self.firstcall = False
                while True:
                    result = self.func(*args, **kwd)
                    if result is self.CONTINUE: # update arguments
                        args, kwd = self.argskwd
                    else: # last call
                        break
            else: # return the arguments of the tail call
                self.argskwd = args, kwd
                return self.CONTINUE
        except: # reset and re-raise
            self.firstcall = True
            raise
        else: # reset and exit
            self.firstcall = True
            return result

def tail_recursive(func):
    &quot;Convert TailRecursive into a signature-preserving decorator&quot;
    return update_wrapper(TailRecursive(func), func, create=True)

#&lt;/_main.py&gt;
</pre>
<p>Here is how you apply the decorator to the good old factorial:</p>
<pre class="literal-block">
#&lt;_main.py&gt;

&#64;tail_recursive
def factorial(n, acc=1):
    &quot;The good old factorial&quot;
    if n == 0: return acc
    return factorial(n-1, n*acc)

#&lt;/_main.py&gt;
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; print factorial(4)
24
</pre>
<p>This decorator is pretty impressive, and should give you some food for
your mind ;) Notice that there is no recursion limit now, and you can
easily compute <tt class="docutils literal"><span class="pre">factorial(1001)</span></tt> or larger without filling the stack
frame. Notice also that the decorator will not work on functions which
are not tail recursive, such as</p>
<pre class="literal-block">
def fact(n): # this is not tail-recursive
    if n == 0: return 1
    return n * fact(n-1)
</pre>
<p>(a function is tail recursive if it either returns a value without
making a recursive call, or returns directly the result of a recursive
call).</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id14" id="caveats-and-limitations" name="caveats-and-limitations">Caveats and limitations</a></h1>
<p>You should be aware that decorators have a performance penalty.
The worse case is shown by the following example:</p>
<pre class="literal-block">
$ cat performance.sh
python -m timeit -s &quot;
from decorator import decorator

&#64;decorator
def do_nothing(func, *args, **kw):
    return func(*args, **kw)

&#64;do_nothing
def f():
    pass
&quot; &quot;f()&quot;

python -m timeit -s &quot;
def f():
    pass
&quot; &quot;f()&quot;
</pre>
<p>On my Linux system, using the <tt class="docutils literal"><span class="pre">do_nothing</span></tt> decorator instead of the
plain function is more than four times slower:</p>
<pre class="literal-block">
$ bash performance.sh
1000000 loops, best of 3: 1.68 usec per loop
1000000 loops, best of 3: 0.397 usec per loop
</pre>
<p>It should be noted that a real life function would probably do
something more useful than <tt class="docutils literal"><span class="pre">f</span></tt> here, and therefore in real life the
performance penalty could be completely negligible.  As always, the
only way to know if there is
a penalty in your specific use case is to measure it.</p>
<p>You should be aware that decorators will make your tracebacks
longer and more difficult to understand. Consider this example:</p>
<pre class="doctest-block">
&gt;&gt;&gt; &#64;tracing
... def f():
...     1/0
</pre>
<p>Calling <tt class="docutils literal"><span class="pre">f()</span></tt> will give you a <tt class="docutils literal"><span class="pre">ZeroDivisionError</span></tt>, but since the
function is decorated the traceback will be longer:</p>
<pre class="doctest-block">
&gt;&gt;&gt; f()
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in ?
    f()
  File &quot;&lt;string&gt;&quot;, line 2, in f
  File &quot;&lt;stdin&gt;&quot;, line 4, in tracing
    return f(*args, **kw)
  File &quot;&lt;stdin&gt;&quot;, line 3, in f
    1/0
ZeroDivisionError: integer division or modulo by zero
</pre>
<p>You see here the inner call to the decorator <tt class="docutils literal"><span class="pre">tracing</span></tt>, which calls
<tt class="docutils literal"><span class="pre">f(*args,</span> <span class="pre">**kw)</span></tt>, and a reference to  <tt class="docutils literal"><span class="pre">File</span> <span class="pre">&quot;&lt;string&gt;&quot;,</span> <span class="pre">line</span> <span class="pre">2,</span> <span class="pre">in</span> <span class="pre">f</span></tt>.
This latter reference is due to the fact that internally the decorator
module uses <tt class="docutils literal"><span class="pre">eval</span></tt> to generate the decorated function. Notice that
<tt class="docutils literal"><span class="pre">eval</span></tt> is <em>not</em> responsibile for the performance penalty, since is the
called <em>only once</em> at function decoration time, and not every time
the decorated function is called.</p>
<p>At present, there is no way to avoid <tt class="docutils literal"><span class="pre">eval</span></tt>. A clean solution
would require to change the CPython implementation of functions and
add an hook to make it possible to change their signature directly.
This may happen in future versions of Python and then this module
would become obsolete.</p>
<p>For debugging purposes, it may be useful to know that the decorator
module also provides a <tt class="docutils literal"><span class="pre">getinfo</span></tt> utility function which returns a
dictionary containing information about a function.
For instance, for the factorial function we will get</p>
<pre class="doctest-block">
&gt;&gt;&gt; d = getinfo(factorial)
&gt;&gt;&gt; d['name']
'factorial'
&gt;&gt;&gt; d['argnames']
['n', 'acc']
&gt;&gt;&gt; d['signature']
'n, acc'
&gt;&gt;&gt; d['defaults']
(1,)
&gt;&gt;&gt; d['doc']
'The good old factorial'
</pre>
<p>In the present implementation, decorators generated by <tt class="docutils literal"><span class="pre">decorator</span></tt>
can only be used on user-defined Python functions or methods, not on generic
callable objects, nor on built-in functions, due to limitations of the
<tt class="docutils literal"><span class="pre">inspect</span></tt> module in the standard library.
Also, there is a restriction on the names of the arguments: if try to
call an argument <tt class="docutils literal"><span class="pre">_call_</span></tt> or <tt class="docutils literal"><span class="pre">_func_</span></tt> you will get an AssertionError:</p>
<pre class="doctest-block">
&gt;&gt;&gt; &#64;tracing
... def f(_func_): print f
...
Traceback (most recent call last):
  ...
AssertionError: You cannot use _call_ or _func_ as argument names!
</pre>
<p>(the existence of these two reserved names is an implementation detail).</p>
<p>Moreover, the implementation is such that the decorated function contais
a copy of the original function attributes:</p>
<pre class="doctest-block">
&gt;&gt;&gt; def f(): pass # the original function
&gt;&gt;&gt; f.attr1 = &quot;something&quot; # setting an attribute
&gt;&gt;&gt; f.attr2 = &quot;something else&quot; # setting another attribute
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; traced_f = tracing(f) # the decorated function
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; traced_f.attr1
'something'
&gt;&gt;&gt; traced_f.attr2 = &quot;something different&quot; # setting attr
&gt;&gt;&gt; f.attr2 # the original attribute did not change
'something else'
</pre>
<p>That's all folks, enjoy!</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id15" id="licence" name="licence">LICENCE</a></h1>
<p>Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:</p>
<pre class="literal-block">
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in bytecode form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
&quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
</pre>
<p>If you use this software and you are happy with it, consider sending me a
note, just to gratify my ego. On the other hand, if you use this software and
you are unhappy with it, send me a patch!</p>
</div>
</div>
</body>
</html>
